package sizeyunsuan;

/**
 * Created by 春旺 on 2017/5/16.
 */
public class RationalNumber {

    private int numerator, denominator;



    //-----------------------------------------------------------------
// Constructor: Sets up the rational number by ensuring a nonzero
// denominator and making only the numerator signed.
//-----------------------------------------------------------------
    public RationalNumber(int numer, int denom) {
        if (denom == 0)
            denom = 1;
// Make the numerator "store" the sign
        if (denom < 0) {
            numer = numer * -1;
            denom = denom * -1;
        }                                             //分母是正数
        numerator = numer;                         //分子
        denominator = denom;                      //分母

        reduce();
    }

    //-----------------------------------------------------------------
// Returns the numerator of this rational number.
//-----------------------------------------------------------------
    public int getNumerator() {
        return numerator;
    }

    //-----------------------------------------------------------------
// Returns the denominator of this rational number.
//-----------------------------------------------------------------
    public int getDenominator() {
        return denominator;
    }

    //-----------------------------------------------------------------
// Returns the reciprocal of this rational number.
//-----------------------------------------------------------------
    public RationalNumber reciprocal() {
        return new RationalNumber(numerator, denominator);
    }

    //-----------------------------------------------------------------
// Adds this rational number to the one passed as a parameter.
// A common denominator is found by multiplying the individual
// denominators.
//-----------------------------------------------------------------
    public RationalNumber add(RationalNumber op2) {
        int commonDenominator = denominator * op2.getDenominator();
        int numerator1 = numerator * op2.getDenominator();
        int numerator2 = op2.getNumerator() * denominator;
        int sum = numerator1 + numerator2;
        return new RationalNumber(sum, commonDenominator);
    }

    //-----------------------------------------------------------------
// Subtracts the rational number passed as a parameter from this
// rational number.
//-----------------------------------------------------------------
    public RationalNumber subtract(RationalNumber op2) {
        int commonDenominator = denominator * op2.getDenominator();
        int numerator1 = numerator * op2.getDenominator();
        int numerator2 = op2.getNumerator() * denominator;
        int difference = numerator1 - numerator2;
        return new RationalNumber(difference, commonDenominator);
    }

    //-----------------------------------------------------------------
// Multiplies this rational number by the one passed as a
// parameter.
//-----------------------------------------------------------------
    public RationalNumber multiply(RationalNumber op2) {
        int numer = numerator * op2.getNumerator();
        int denom = denominator * op2.getDenominator();
        return new RationalNumber(numer, denom);
    }

    //-----------------------------------------------------------------
// Divides this rational number by the one passed as a parameter
// by multiplying by the reciprocal of the second rational.
//-----------------------------------------------------------------
    public RationalNumber divide(RationalNumber op2) {
        RationalNumber op1 = op2.reciprocal();
        int numer = numerator * op1.getNumerator();
        int denom = denominator * op1.getDenominator();
        return new RationalNumber(numer, denom);
    }

    //-----------------------------------------------------------------
// Determines if this rational number is equal to the one passed
// as a parameter. Assumes they are both reduced.
//-----------------------------------------------------------------
    public boolean isLike(RationalNumber op2) {
        return (numerator == op2.getNumerator() &&
                denominator == op2.getDenominator());
    }

    //-----------------------------------------------------------------
// 返回计算结果
//-----------------------------------------------------------------
    public String toString() {
        String result;
        if (numerator == 0)
            result = "0";
        else if (denominator == 1)
            result = numerator + "";
        else
            result = numerator + "/" + denominator;
        return result;
    }

    //-----------------------------------------------------------------
// 化简，找到最大公约数，并将分数化成最简分数。
//-----------------------------------------------------------------
    private String reduce() {
        if (numerator != 0) {
            int common = gcd(Math.abs(numerator), denominator);
            numerator = numerator / common;
            denominator = denominator / common;
        }
        return numerator + "/"+ denominator;
    }

    //-----------------------------------------------------------------
// 找出最大公约数
//-----------------------------------------------------------------
    private int gcd(int num1, int num2) {
        while (num1 != num2)
            if (num1 > num2)
                num1 = num1 - num2;
            else
                num2 = num2 - num1;
        return num1;
    }
}

